package 力扣.双指针.定长区间;

import java.util.HashMap;

public class 字符串排列567 {
// 统计类

    class Counter extends HashMap<Character, Integer> {

        public int get(Character k) {
            return containsKey(k) ? super.get(k) : 0;
        }

        public void add(Character k, int v) {
            put(k, get(k) + v);
            if (get(k) <= 0) {
                remove(k);
            }
        }
    }

    boolean checkInclusion(String A, String B) {

        // B contains A?
        final int blen = B == null ? 0 : B.length();
        final int alen = A == null ? 0 : A.length();

        // 统计A字符串里面每个字符的出现次数。
        Counter astat = new Counter();

        for (int i = 0; i < alen; i++) {
            astat.add(A.charAt(i), 1);
        }

        // 统计B字符串里面每个字符的出现次数。
        Counter bstat = new Counter();
        int left = -1;
        // 比较bstat与astat，有多少个字符的统计结果已经是相等的。
        int equal = 0;
        for (int i = 0; i < blen; i++) {
            // 将B[i]添加到区间中
            Character c = B.charAt(i);

            // 更新区间的状态
            bstat.add(c, 1);

            // 更新两个计数器相等的计数项的个数
            if (bstat.get(c) == astat.get(c)) {
                equal++;
            }

            // 如果窗口太小
            if (i - left < alen) {
                continue;
            }

            // assert 此时(left, i]长度必然等于windowSize
            // DONE 判断区间的状态是否满足约
            if (equal == astat.size()) {
                return true;
            }
            // 移除滑动窗口的尾巴!
            left++;
            // 更新移动后的区间的状态
            Character rm = B.charAt(left);
            if (bstat.get(rm) == astat.get(rm)) {
                equal--;
            }
            bstat.add(rm, -1);
        }
        return false;
    }

    class Counter2 extends HashMap<Character,Integer>{
        public int get(char key){//计数器类型不能弄错了！
            return containsKey(key) ? super.get(key) : 0;
        }
        public void add(char key, int value){
            put(key, get(key) + value);
            if (get(key) <= 0){
                remove(key);
            }
        }
    }
    public boolean checkInclusion2(String s1, String s2) {
          if (s1 == null || s1.length() == 0 || s2 == null || s2.length() == 0){
              return false;
          }
          final  int len1 = s1.length();
          final  int len2 = s2.length();
        Counter2 count1 = new Counter2();
        for (int i = 0; i < len1; i++) {
            count1.add(s1.charAt(i), 1);
        }
        Counter2 counter2 = new Counter2();
        int left = -1;
        int eqNums = 0;
        for (int i = 0; i < len2; i++) {
            char c = s2.charAt(i);
            counter2.add(c, 1);
            if (count1.get(c) == counter2.get(c)){
                eqNums++;//先进行++
            }
            if (i - left < len1){//长度必须为len1
                continue;
            }
            if (eqNums == count1.size()){
                return true;
            }
            left++;
            char cLeft = s2.charAt(left);
            if (count1.get(cLeft) == counter2.get(cLeft)){
                eqNums--;
            }
            counter2.add(cLeft, -1);//长度必须为len1
        }
        return false;
    }

}
